package com.pxst.bank.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxst.bank.entity.SysAvailableBankEntity;
import com.pxst.bank.entity.SysBankCardEntity;
import com.pxst.bank.entity.SysBankCardFlowEntity;
import com.pxst.bank.mapper.SysAvailableBankMapper;
import com.pxst.bank.mapper.SysBankCardMapper;
import com.pxst.bank.service.SysBankCardFlowEntityService;
import com.pxst.bank.mapper.SysBankCardFlowEntityMapper;
import com.pxst.exception.BusinessException;
import com.pxst.resp.ApiResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author Administrator
 * @description 针对表【sys_bank_card_flow(银行卡流水(bc110))】的数据库操作Service实现
 * @createDate 2023-09-13 17:36:31
 */
@Service
public class SysBankCardFlowEntityServiceImpl extends ServiceImpl<SysBankCardFlowEntityMapper, SysBankCardFlowEntity>
        implements SysBankCardFlowEntityService {

    @Autowired
    private SysBankCardFlowEntityMapper sysBankCardFlowEntityMapper;

    @Autowired
    private SysBankCardMapper sysBankCardMapper;

    @Autowired
    private SysAvailableBankMapper sysAvailableBankMapper;


    @Override
    public ApiResp checkLimit(Integer from, Integer to, BigDecimal requestAmount) {
        /**
         * 查询出发起者的流水信息
         */
        LambdaQueryWrapper<SysBankCardFlowEntity> wrapperFrom = new LambdaQueryWrapper<>();
        wrapperFrom.eq(SysBankCardFlowEntity::getBankCardId, from);
        SysBankCardFlowEntity fromer = sysBankCardFlowEntityMapper.selectOne(wrapperFrom);

        /**
         * 查询出发起者的银行卡信息
         */
        SysBankCardEntity fromBank = sysBankCardMapper.selectById(from);


        /**
         * 查询出接收者的流水信息
         */
        LambdaQueryWrapper<SysBankCardFlowEntity> wrapperTo = new LambdaQueryWrapper<>();
        wrapperTo.eq(SysBankCardFlowEntity::getBankCardId, to);
        SysBankCardFlowEntity toer = sysBankCardFlowEntityMapper.selectOne(wrapperTo);
        /**
         * 查询出接收者的银行卡信息
         */
        SysBankCardEntity toBank = sysBankCardMapper.selectById(to);


        /**
         * 0.判断是否查到银行卡流水记录
         */
        if (ObjectUtil.isAllNotEmpty(fromer, toer)) {
            //1.判断发起者的日转出金额上限
            if (fromer.getDayFlowOut().add(requestAmount).compareTo(fromBank.getDayStreamOutMax()) > 0) {
                //判断此次转账是否超过日转上限（是）
                throw new BusinessException("本次转账超过转账者日转上限！");
            }

            //2.判断发起者的月转出金额上限
            if (fromer.getMonthFlowOut().add(requestAmount).compareTo(fromBank.getMonthStreamOutMax()) > 0) {
                //判断此次 转账是否超过月转上限（是）
                throw new BusinessException("本次转账超过转账者月转上限！");
            }

            //3.判断发起者的日转出次数
            if (fromer.getDaySuccessOutCount() + 1 > fromBank.getDayThresholdMax()) {
                throw new BusinessException("本次转账超过发起者日转次数！");
            }
            //4.判断发起者的月转出次数
            if (fromer.getMonthSuccessOutCount() + 1 > fromBank.getMonthThresholdMax()) {
                throw new BusinessException("本次转账超过发起者月转次数！");
            }

            //5.判断发起者的健康额度
            if (fromer.getBalance().subtract(requestAmount).compareTo(BigDecimal.valueOf(0)) < 0) {
                throw new BusinessException("本次转账超过发起者可用余额！");
            }

            //6.判断接收者的日接收金额数量
            if (toer.getDayFlowIn().add(requestAmount).compareTo(toBank.getDayDepositMax()) > 0) {
                throw new BusinessException("本次转出超过接收者的日接收总金额！");
            }

            //7.判断接收者日接收次数
            if (toer.getDaySuccessInCount() + 1 > toBank.getDayThresholdMax()) {
                throw new BusinessException("本次转出超过接受者的日接收总次数！");
            }

            //8.判断接收者的月接收次数
            if (toer.getMonthSuccessInCount() + 1 > toBank.getMonthThresholdMax()) {
                throw new BusinessException("本次转出超过接受者的月接收总次数！");
            }

            /**
             * 在存在记录的添加下进行银行内转条件的查询
             */
            SysAvailableBankEntity fromBankInfo = sysAvailableBankMapper.selectById(fromer.getBankCardId());

            SysAvailableBankEntity toBankInfo = sysAvailableBankMapper.selectById(toer.getBankCardId());

            //9.判断本次转账是否超过发起者的最大内转和最小内转
            if (fromBankInfo.getTrMax().compareTo(requestAmount) < 0 || fromBankInfo.getTrMin().compareTo(requestAmount) > 0) {
                throw new BusinessException("超过银行内转限额！");
            }

            //10.判断本次转账是否超过接受者的最大内转和最小内转
            if (toBankInfo.getTrMax().compareTo(requestAmount) < 0 || toBankInfo.getTrMin().compareTo(requestAmount) > 0) {
                throw new BusinessException("超过银行内转限额！");
            }

            /**
             * 判断银行卡状态
             */
            /**
             * 这里还有一个银行卡类型的判断没做（因为不太理解类型判断的条件什么后续找打了再加）
             */

            //11.判断两个银行卡是否是启用状态
            if (fromBank.getStatus() != 1 && toBank.getStatus() != 1) {
                throw new BusinessException("银行卡状态异常！");
            }
        } else {
            throw new BusinessException("账户数据异常！");
        }

        /**
         *检查通过返回可执行转账通知
         */
        return ApiResp.sucess();
    }
}




